package graphics.engine;

import java.nio.*;
import java.util.*;

import javax.media.opengl.*;

import colors.Color;

import utils.*;


public class VertexBufferObject {

	static final int floatSizeInByte = 4;
	static final int shortSizeInByte = 2;
	
	public int VertexBufferID;
	public int IndexBufferID;
	public int ColorBufferID;
	public int TextureCoordBufferID;
	
	public int vertexCount;
	public int indexCount;
	
	public void GenerateVertexBuffer(GL gl, ArrayList<Point3D> list)
	{
		if(list.size() == 0)
		{
			return ;
		}
		
		int[] id = new int[1];	
		int nbFloat = 3;
		int bufferSize = list.size() * nbFloat * floatSizeInByte;
		
		FloatBuffer buffer = FloatBuffer.allocate(list.size() * nbFloat);
		for(int i =0; i < list.size(); i++)
		{
			buffer.put((float)list.get(i).x());
			buffer.put((float)list.get(i).y());
			buffer.put((float)list.get(i).z());
		}
		buffer.position(0);
		
		gl.glGenBuffers(1, id,0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, id[0]);
		gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferSize, buffer, GL.GL_STATIC_DRAW);
		
		buffer.clear();
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		this.vertexCount = list.size(); 
		
		this.VertexBufferID = id[0];
	}
	public void GenerateColorBuffer(GL gl, ArrayList<Color> list)
	{
		if(list.size() == 0)
		{
			return ;
		}
		
		int[] id = new int[1];	
		int nbFloat = 4;
		int bufferSize = list.size() * nbFloat * floatSizeInByte;
		
		FloatBuffer buffer = FloatBuffer.allocate(list.size() * nbFloat);
		for(int i =0; i < list.size(); i++)
		{
			buffer.put((float)list.get(i).R);
			buffer.put((float)list.get(i).G);
			buffer.put((float)list.get(i).B);
			buffer.put((float)list.get(i).A);
		}
		buffer.position(0);
		
		gl.glGenBuffers(1, id,0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, id[0]);
		gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferSize, buffer, GL.GL_STATIC_DRAW);
		buffer.clear();
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		this.ColorBufferID = id[0];
	}
	public void GenerateIndexBuffer(GL gl, short[] list)
	{
		if(list.length == 0)
		{
			return ;
		}
		
		int[] id = new int[1];	
		int bufferSize = list.length * shortSizeInByte;
		
		ShortBuffer buffer = ShortBuffer.allocate(list.length);
		buffer.put(list);
		buffer.position(0);
		
		gl.glGenBuffers(1, id,0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, id[0]);
		gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferSize, buffer, GL.GL_STATIC_DRAW);
		
		buffer.clear();
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		this.indexCount = list.length;
		this.IndexBufferID = id[0];
	}
	public void GenerateTextureBuffer(GL gl, ArrayList<Point2D> texturesCoords) 
	{
		if(texturesCoords.size() == 0)
		{
			return ;
		}
		
		int[] id = new int[1];	
		int nbFloat = 2;
		int bufferSize = texturesCoords.size() * nbFloat * floatSizeInByte;
		
		FloatBuffer buffer = FloatBuffer.allocate(texturesCoords.size() * nbFloat);
		for(int i =0; i < texturesCoords.size(); i++)
		{
			buffer.put((float)texturesCoords.get(i).x());
			buffer.put((float)texturesCoords.get(i).y());
		}
		buffer.position(0);
		
		gl.glGenBuffers(1, id,0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, id[0]);
		gl.glBufferData(GL.GL_ARRAY_BUFFER, bufferSize, buffer, GL.GL_STATIC_DRAW);
		
		buffer.clear();
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		this.TextureCoordBufferID = id[0];		
	}

	public String toString()
	{
		return "VertexBufferID: " + VertexBufferID + " / IndexBufferID: " + IndexBufferID + " / ColorBufferID: " + ColorBufferID + " / indexCount: " + indexCount + " / vertexCount :" + vertexCount;
	}
}
